package hydrap2p.proxy.manager;

import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.thrift.TException;

import hydrap2p.helper.ServiceConnector;
import hydrap2p.modulemanager.ModuleControl;

public class ModuleControlListener implements ModuleControl.Iface {

	public ModuleControlListener()
	{
		timer = new Timer();
		timer.schedule(new PingTimer(), pingerdelay, pingerdelay);
	}
	
	@Override
	public void stop() {
		Main.moduleController.shutdown();
		System.exit(0);
	}

	@Override
	public void ping() throws TException {
		
	}

	@Override
	public List<String> supportedInterfaces() throws TException {
		return Arrays.asList("Service", "ModuleControl");
	}

	private Timer timer;
	private static final long pingerdelay = 10000;
	
	private class PingTimer extends TimerTask {

		@Override
		public void run() {
			try {
				ServiceConnector.getServiceManager().ping();
			} catch (Exception e) {
				stop();
			}
		}
	}

}
